package com.example.administrator.myapplication.utils;

import android.content.Context;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

public class MyUtils {
    public static boolean saveInfo(Context context, String username, String password)   {
        //new File("/data/data/com.example.administrator.myapplication");
        //建议使用上下文获取常用目录
        try{
            String path=context.getFilesDir().getPath();
            File file=new File(path,"userinfo.txt");
            if(file.exists()){
                file.delete();
            }
            file.createNewFile();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
            bufferedWriter.write(username+"#"+password);
            bufferedWriter.close();
        }catch(Exception e){
            return false;
        }
        return true;
    }


    public static Map<String,Object> getInfo(Context context){

        Map<String,Object> map=new HashMap<String,Object>();
        try{
            String path=context.getFilesDir().getPath();
            File file=new File(path,"userinfo.txt");
            if(file.exists()){
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                String line=bufferedReader.readLine();
                String arr[]=line.split("#");
                if(arr!=null){
                    map.put("username",arr[0]);
                    map.put("password",arr[1]);
                }
                return map;
            }else{
                return null;
            }
        }catch (Exception e){
            return null;
        }
    }
}
